import {patch, cascade, fetch} from '../../../utils/axios'

const state = {
  name: '编辑项目',
  valid: null,
  showPubcmd: false,
  showDockercmd: false,
  showJavaopt: false,
  showDockeropt: false,
  showBuildParam: false,
  showPath: false,
  parent: {},
  project_nature: {},
  projectType: {},
  status: {},
  publish_type: {},
  build_param: {},
  pub_version: {},
  detection: {},
  env: {},
  owner: {},
  tempEnv: '',
  tempPublishType: null,
  fields: {},
  pubcmd: {},
  dockercmd: {},
  javaopt: {},
  dockeropt: {},
  service: {},
  host: {items: [{ text: '', value: '' }]},
  nameRules: [
    (v) => !!v || '请输入项目名'
  ],
  name_enRules: [
    (v) => !!v || '请输入项目英文名'
  ],
  projectTypeRules: [
    (v) => !!v || '请选择项目类型'
  ],
  envRules: [
    (v) => !!v || '请选择项目环境'
  ],
  gitPath: [
    (v) => !!v || '请填写git地址'
  ],
  pubcmdRules: [
    (v) => { if (state.showPubcmd && !v) { return '请选择发布参数' } return true }
  ],
  javaoptRules: [
    (v) => { if (state.showJavaopt && !v) { return '请选择JAVA参数' } return true }
  ],
  dockercmdRules: [
    (v) => { if (state.showDockercmd && !v) { return '请选择容器发布参数' } return true }
  ],
  dockeroptRules: [
    (v) => { if (state.showDockeropt && !v) { return '请选择容器JAVA参数' } return true }
  ],
  relative_conf_pathRules: [
    (v) => { if (state.showPath && !v) { return '请填写文件路径' } return true }
  ],
  absolute_conf_pathRules: [
    (v) => { if (state.showPath && !v) { return '请填写文件路径' } return true }
  ],
  pubverRules: [
    (v) => v !== undefined || '请选择发布版本'
  ],
  ownerRules: [
    (v) => !!v || '请选择负责人'
  ],
  detectionRules: [
    (v) => v !== undefined || '请选择检测方式'
  ],
  detectionMethodRules: [
    (v) => !!v || '请填写检测地址'
  ],
  msg: {
    name: '',
    name_en: '',
    projectType: '',
    env: ''
  }
}

const mutations = {
  editProject (state, payload) {
    if (state.fields.parent === undefined) {
      state.fields.parent = ''
    }
    if (payload.validate()) {
      var url = '/project/' + state.fields.id + '/'
      patch(url,
        state.fields
      ).then((response) => {
        this._vm.$data.$$state.listproject.editdialog = false
      }).catch((error) => {
        if (error.response) {
          let datas = error.response.data
          for (var name in datas) {
            state.msg[name] = datas[name]
          }
        }
      })
    }
  },
  deleteProject (state) {
    let r = confirm('确认删除？')
    if (r === true) {
      var url = '/project/' + state.fields.id + '/'
      cascade(url).then((response) => {
        this._vm.$data.$$state.listproject.editdialog = false
      }).catch((error) => {
        console.log(error.response.data.detail)
      })
    }
  },
  closeEditProject (state) {
    this._vm.$data.$$state.listproject.editdialog = false
    let msg = this.msg
    for (var i in msg) {
      msg[i] = ''
    }
  },
  loadEditProject (state, payload) {
    let url = '/project/' + payload + '/'
    fetch(url, {
    }).then((response) => {
      state.fields = response
      state.tempEnv = state.fields.env
      state.tempPublishType = state.fields.publish_type
    }).catch((error) => {
      console.log(error.response.data.detail)
    })
    state.project_nature = this._vm.$data.$$state.listproject.project_nature
    state.projectType = this._vm.$data.$$state.listproject.projectType
    state.status = this._vm.$data.$$state.listproject.status
    state.publish_type = this._vm.$data.$$state.listproject.publish_type
    state.env = this._vm.$data.$$state.listproject.env
    state.owner = this._vm.$data.$$state.listproject.owner
    state.build_param = this._vm.$data.$$state.listproject.build_param
    state.pub_version = this._vm.$data.$$state.listproject.pub_version
    state.detection = this._vm.$data.$$state.listproject.detection
  },
  loadProjectParent (state, payload) {
    let url = '/project/project_parent/'
    fetch(url, {
      opt: payload
    }).then((response) => {
      let res = JSON.parse(response)
      state.parent.items = res.data
    }).catch((error) => {
      console.log(error.response.data.detail)
    })
  },
  editWatchPublishType (state) {
    if (state.tempPublishType !== state.fields.publish_type) {
      state.fields.pubcmd = null
      state.fields.javaopt = null
    }
    if (state.fields.publish_type === 1) {
      state.showPubcmd = true
      state.showJavaopt = true
    } else {
      state.showPubcmd = false
      state.showJavaopt = false
    }
  },
  editProjectGetHost (state) {
    if (state.tempEnv !== state.fields.env) {
      state.fields.host = []
    }
    state.host = {items: [{ text: '', value: '' }]}
    fetch('/project/get_host/', {
      env: state.fields.env,
      id: state.fields.id
    }).then((response) => {
      let res = JSON.parse(response)
      state.host.items = res.data
    }).catch((error) => {
      state.etext = error.response
    })
  },
  editProjectGetService (state) {
    if (state.tempEnv !== state.fields.env) {
      state.fields.service = []
    }
    state.service = {items: [{ text: '', value: '' }]}
    fetch('/project/get_service/', {
      env: state.fields.env
    }).then((response) => {
      let res = JSON.parse(response)
      state.service.items = res.data
    }).catch((error) => {
      state.etext = error.response
    })
  },
  editProjectGetPubcmd (state) {
    if (state.tempEnv !== state.fields.env || state.tempPublishType !== state.fields.publish_type) {
      state.fields.pubcmd = null
    }
    state.pubcmd = {items: [{text: '', value: ''}]}
    fetch('/project/get_pubcmd/', {
      env: state.fields.env
    }).then((response) => {
      let res = JSON.parse(response)
      state.pubcmd.items = res.data
    }).catch((error) => {
      state.etext = error.response
    })
  },
  editProjectGetJavaopt (state) {
    if (state.tempPublishType !== state.fields.publish_type) {
      state.fields.javaopt = null
    }
    state.javaopt = {items: [{text: '', value: ''}]}
    fetch('/project/get_javaopt/', {

    }).then((response) => {
      let res = JSON.parse(response)
      state.javaopt.items = res.data
    }).catch((error) => {
      state.etext = error.response
    })
  },
  editPubVer (state) {
    switch (state.fields.pub_version) {
      case 0:
        state.showBuildParam = false
        state.showDockeropt = false
        state.showDockercmd = false
        state.fields.docker_opt = ''
        state.fields.docker_cmd = ''
        break
      case 1:
        state.showBuildParam = true
        state.showDockeropt = false
        state.showDockercmd = false
        state.fields.docker_opt = ''
        state.fields.docker_cmd = ''
        break
      case 2:
        state.showBuildParam = true
        state.showDockeropt = true
        state.showDockercmd = true
        this.commit('editproject/getDockerOpt')
        if (state.fields.env) {
          this.commit('editproject/getDockerCmd')
        }
    }
  },
  getDockerOpt (state) {
    state.fields.dockeropt = null
    state.dockeropt = {items: [{text: '', value: ''}]}
    fetch('/project/get_dockeropt/', {

    }).then((response) => {
      let res = JSON.parse(response)
      state.dockeropt.items = res.data
    }).catch((error) => {
      state.etext = error.response
    })
  },
  getDockerCmd (state) {
    state.fields.dockercmd = null
    state.dockercmd = {items: [{text: '', value: ''}]}
    fetch('/project/get_dockercmd/', {
      env: state.fields.env
    }).then((response) => {
      let res = JSON.parse(response)
      state.dockercmd.items = res.data
    }).catch((error) => {
      state.etext = error.response
    })
  }
}

const actions = {
  editProject ({commit, state}, payload) {
    commit('editProject', payload)
  },
  deleteProject ({commit}) {
    commit('deleteProject')
  },
  closeEditProject ({commit}) {
    commit('closeEditProject')
  },
  loadEditProject ({commit}, payload) {
    commit('loadEditProject', payload)
    commit('loadProjectParent', payload)
  },
  editWatchPublishType ({commit, state}, payload) {
    if (payload) {
      commit('editWatchPublishType')
      commit('editProjectGetJavaopt')
      if (state.showPubcmd && state.fields.publish_type === 1) {
        commit('editProjectGetPubcmd')
      }
    }
  },
  editWatchProjectEnv ({commit}, payload) {
    if (payload) {
      commit('editProjectGetHost')
      commit('editProjectGetService')
      if (state.showPubcmd && state.fields.publish_type === 1) {
        commit('editProjectGetPubcmd')
      }
      if (state.showDockercmd) {
        commit('getDockerCmd')
      }
    }
  },
  editPubVer ({commit}) {
    commit('editPubVer')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
